require "Net/send_Msg"
require "Net/TrialHandel"
require 'View/UIBase/UIBase'

UIOperationNotice = UIBase.New(PanelResNames.UIOperationNotice);
local this = UIOperationNotice

function UIOperationNotice.Init(data)
    if data then
        this.constraint = data.constraint
        this.callback = data.callback
    end
    
    this.data = logicMgr.NoticeManager.GetNoticeList()
    
    UIBase.Init(UIOperationNotice);
end

--定义控件
function UIOperationNotice:OnCreate()
    --self.titleText = self:GetChild("title");
    --self.titleTopText = self:GetChild("titletop");
    --self.titleDecText = self:GetChild("txt"):GetChildAt(0):GetChild("titletext");
    self.backBtn = self:GetChild("n1")
    self.bgbackBtn = self:GetChild("n4")
    self.descList = self:GetList("NoticeText")
    self.tabList = self:GetList("TabList")
    self.titleText = self:GetChild("NoticeTitle")
    self.ctrlBannerType = self:GetController("bannertype")
    self.typeController = self:GetController("type")
    self.oldSelectBtn = nil
    self:SetDynamicList(self.tabList,self.ItemRender)
    self:SetDynamicList(self.descList,self.DescRender,self,true)
    self:addEvent();
    self.pageIndex = 1
    self.backBtn.sound = ""
    self.bgbackBtn.sound = ""
    if this.constraint then
        self.typeController.selectedIndex = 0;
    else
        self.typeController.selectedIndex = 1;
    end
    self:RefreshPageIndex()
end

function UIOperationNotice:RefreshPageIndex()
    if this.constraint then
        for k,v in pairs(UIOperationNotice.data) do
            if v.push == 1 then
                self.pageIndex = k
            end
        end
    end
end

function UIOperationNotice:OnEnable()
    self:refresh()
end
function UIOperationNotice:RefreshContent()
    self.descList.numItems = 1
    local data = UIOperationNotice.data[self.pageIndex]
    if not data then
        return
    end
    self.titleText.text = data.title
    self.ctrlBannerType.selectedIndex = tonumber(data.type) - 1
end
function UIOperationNotice:refresh()
    self:RefreshPageIndex()
    self.tabList.numItems = #UIOperationNotice.data
    self:RefreshContent()
    --[[local url ="http://fsgame.xiaoniuhy.com/notice/android/"..Config.SDK_Channel.."/notice.txt"
    Http.Get(url, nil, function(success, str)
        if not success then
            logError("http get fight cmd failed. url: " .. url .. ". ErrMsg: " .. cmdStr)
            onLoaded(nil)
            return
        end
        self.titleDecText.text = str;

    end)]]

end
function UIOperationNotice:DescRender(index,go)
    local desc = go:GetChild("titletext")
    local data = UIOperationNotice.data[self.pageIndex]
    if not data then
        return
    end
    desc.text = UIOperationNotice.FilterStr(data.content)
    desc.onClickLink:Remove(UIOperationNotice.OnClickLinkUrl)
    desc.onClickLink:Add(UIOperationNotice.OnClickLinkUrl)
end

local kk = { ["&lt;"] = "<", ["&gt;"] = ">", ["&amp;"] = "&", ["&nbsp;"] = " ", ["&lt;/br&gt;"] = "/n"}

local fiter_tab = {}
function UIOperationNotice.FilterStr(str)
    local t1 = string.gsub(str, "&lt;", "<")
    local t2 = string.gsub(t1, "&gt;", ">")
    local t3 = string.gsub(t2, "%b<>", "") -- &lt;p&gt;fgfds&lt;/p&gt;   <p>fgfds</p>
    local tstr = string.gsub(t3, "&amp;nbsp;", " ")
    -- local tt = string.match('&lt;%w*%p/&gt;', str)
    -- print(tt)
    return tstr
end

---@param go FairyGUI.GButton
function UIOperationNotice:ItemRender(index,go)
    local data = UIOperationNotice.data[index]
    local desc = go:GetChild("Desc")
    local ctr_red = go:GetChild("UIRedDots"):GetController("c1")
    local ctr_tabtype = go:GetController("tabtype")

    ctr_tabtype.selectedIndex = tonumber(data.type) - 1
    desc.text = string.gsub(data.tag, "&lt;br/&gt;", "\n")
    if self.pageIndex == index then
        self:ChangeSelect(self.pageIndex,go)
    else
        go.selected = false
    end

    if logicMgr.NoticeManager.CheckRedPoint(data.version) and self.pageIndex ~= index then
        ctr_red.selectedIndex = 2
    else
        ctr_red.selectedIndex = 0
    end

    go.onClick:Clear()
    go.onClick:Add(function()
        self:ChangeSelect(index,go)
        self:RefreshContent()
        ctr_red.selectedIndex = 0
        logicMgr.NoticeManager.ClickNotice(data.version)
    end)
end
function UIOperationNotice:ChangeSelect(index,newGo)
    if self.oldSelectBtn then
        self.oldSelectBtn.selected = false
    end
    newGo.selected = true
    self.oldSelectBtn = newGo
    self.pageIndex = index
end
function UIOperationNotice:OnDisable()
    -- if this.callback then
    --     this.callback()
    -- end

    -- this.callback = nil
end

function UIOperationNotice:OnDestroy()
    
end

function UIOperationNotice:addEvent()
    self:RegisterOnClick(self.backBtn, self.onCloseClick);
    self:RegisterOnClick(self.bgbackBtn, self.onCloseClick)
    
end

function UIOperationNotice:removeEvent()

end

function UIOperationNotice.OnClickLinkUrl(content)
    if content and not string.IsNilOrEmpty(content.data) then 
        UnityEngine.Application.OpenURL(tostring(content.data))
    end
end

function UIOperationNotice:onCloseClick()
    closeUI(PanelNames.UIOperationNotice);
    if this.callback then
        this.callback()
    else
        AudioManager.Play(1052)
    end

    this.callback = nil
end

return UIOperationNotice;